var app = getApp();
const util = require("../../utils/util.js");
Component({
  properties: {
    audio: {
      type: String,
      observer: function(newValue, oldValue) {
        this.data.audioCtx = wx.createInnerAudioContext(),
          this.setData({
            ['audioCtx.src']: newValue,
            ['audioCtx.autoplay']: true,
            ['audioCtx.loop']: true
          })
      }
    }
  },
  data: {
    buttonClicked: false,
    audioCtx: "",
    play_state: true
  },
  lifetimes: {
    attached() {},
    detached() { //离开页面
      this.music_stop();
    },
  },
  methods: {
    change_music: function() { //改变播放状态
      if (this.data.buttonClicked == true) {
        return;
      } else {
        util.buttonClicked(this);
      }

      if (this.data.play_state) {
        this.data.audioCtx.pause();
      } else {
        this.data.audioCtx.play();
      }
      this.setData({
        play_state: !this.data.play_state
      })
    },
    music_stop: function() {
      this.data.audioCtx.stop();
    }
  }
})
